home *** CD-ROM | disk | FTP | other *** search
- /*
- atcnv.c -- At-Sign Conversion Program
-
- This program converts the OPERATOR.ASC file to contain the embedded printer
- codes. Embedded printer codes are for the HP-LaserJet II series of printers.
- it should be easy to change this to work with other types of printers, just
- change the spcode and epcode array and recompile the program. Note the
- character in the string array is automatically changed to an "Escape"
- Character if it is the "@" character. Written in TURBO C, by Steve Booth.
- EMail: EXEC-Pc.
-
- This code is freeware with no strings attached except that I don't want it
- released commercially (it would be too embarassing).
- */
- #define NBRCDES 30
- #define NBRITM 5
- #include <stdio.h>
- #include <conio.h>
- #include <dos.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* Subroutines: */
-
- int strchk (char *, char *);
- char *strcmatch(char *, char *);
- int strochg(char *, int, int);
-
-
- void main()
- {
- char dli[250]; /* data line (input) */
- char dlo[250]; /* data line (output) */
-
- char epcode[NBRITM][NBRCDES] = {
- "@(s0S", /* End Italic */
- "@(s0B", /* End Bold */
- "\0", /* Indent 5 */
- "\0", /* Subtract 5 */
- "@(s0B" }; /* Page Eject */
-
- char filin[40] = "OPERATOR.ASC"; /* File Name: Input */
- char filout[40] = "TEXT.OUT"; /* File Name: Output */
- FILE *fptri; /* file pointer: input */
- FILE *fptro; /* file pointer: output */
- int idx = 0;
- int len;
-
- char lst[NBRITM][10]= {
- "I(",
- "b(",
- "BEGIN(",
- "END(",
- "SECTION(" };
-
- char *ptr;
- char *ptri;
- char *ptro;
- char *ptr2;
- int ret;
-
- char spcode[NBRITM][NBRCDES] = {
- "@(8U@(s1S", /* Start Italic */
- "@(8U@(s3B", /* Start Bold */
- "@(8U@&a6L", /* Indent 5 */
- "@(8U@&a1L", /* Subtract 5 */
- " @(8U@(s3B"}; /* Page Eject - Stuffed Later + Bold */
-
-
- spcode[4][0] = 12; /* Place Page Eject into Array */
- while (idx <= NBRITM) { /* Insert Escape code into Array */
- strochg(epcode[idx], '@', 27);
- strochg(spcode[idx], '@', 27);
- idx++;
- }
- printf("atcnv - at-sign conversion program for HP-LJ printer.\n");
- printf(" written by S. Booth on 07/15/90\n\n");
-
- printf("Opening file: %s, for reading.\n",filin);
- if ( (fptri = fopen(filin,"r")) == NULL ) {
- fprintf(stderr,"Error opening fileto read.");
- _exit(1);
- }
- printf("Creating file: %s.\n",filout);
- if ( (fptro = fopen(filout,"w")) == NULL ) {
- fprintf(stderr,"Error opening file to write.");
- fclose(fptri);
- _exit(1);
- }
-
- while ( fgets(dli,250,fptri) != NULL ) /* Read in Data Line */
- {
- ptri = dli;
- ptro = dlo;
- dlo[0] = '\0';
- while ( (ptr = strchr(ptri,'@')) != NULL ) {
-
- /* Something to process. */
-
- ptr++;
- idx = 0;
-
- while (idx <= NBRITM) { /* Look for a Matching Name */
-
- ret = strchk(ptr, lst[idx]);
- if (!ret) break;
- idx++;
- } /* End While (idx) */
-
- /* at this point, the list of all valid entries has been searched.
- ret will tell if we've been successful. */
-
- if (!ret) { /* Match Found */
- ptr -= 2; /* position to just before the match */
- len = ptr - ptri + 1; /* number of characters to copy */
- memcpy(ptro, ptri, len); /* first, copy string up to point of match */
- ptro += len; /* new position in "dlo" */
- ptri = ptr + strlen(lst[idx]) + 2; /* new position in "dli" */
- len = strlen(spcode[idx]); /* add in starting printer codes */
- memcpy(ptro, spcode[idx], len); /* Copy Printer Codes */
- ptro += len;
- ptr2 = strcmatch(ptri, "()"); /* Find closing delimiter */
- len = ptr2 - ptri; /* Number of characters between */
- memcpy(ptro, ptri, len); /* Copy input data */
- ptro += len;
- ptri = ptr2 + 1;
-
- if ( epcode[idx] ) { /* More processing */
- len = strlen(epcode[idx]);
- memcpy(ptro,epcode[idx],len);
- ptro += len;
- }
-
- } /* Match Found */
- } /* End While (ptr2) */
-
- len = strlen(ptri) + 1;
- memcpy(ptro, ptri, len);
- fputs(dlo, fptro); /* Write Data Line */
- }
- fputs(spcode[4],fptro);
- fclose(fptri);
- fclose(fptro);
-
- } /* End of main */
-
- int strchk(char *str, char *str2)
- /*
- strchk checks "n" characters of str2 for matching.
- returns 1 = no match.
- 0 = match. */
- {
- int idx = 0;
-
- while (str2[idx]) {
- if ( str[idx] != str2[idx] ) return(1);
- idx++;
- }
- return(0);
- }
-
- char *strcmatch(char *str, char *str2)
- /*
- strcmatch checks "n" characters of str2 for matching.
- returns pointer to matching character; NULL if no match.
-
- str = string to be searched (assumed to be zero terminated).
- str2 = matching pair. E.g., "()".
- */
- {
- int idx = 0;
- int flgctr = 0;
- char *ptr;
-
- while (str[idx]) {
- if ( str[idx] != str2[1] ) {
- if ( str[idx] == str2[0]) flgctr++;
- }
- else {
- ptr = str + idx;
- if ( !flgctr )
- return(ptr);
- else
- flgctr--;
- }
- idx++;
- }
- return(0);
- }
- int strochg(char *str1, int schr, int rchr)
- /*
- This routine changes all occurrences of "schr" into "rchr" in
- str1.
-
- str1 = string to be modified.
- schr = integer character to be replaced.
- rchr = integer replacement character.
-
- The number of changes made is returned.
- */
- {
- int idx = 0;
- int cnt = 0;
-
- while (str1[idx]) {
- if ( str1[idx] == schr ) {
- cnt++;
- str1[idx] = rchr;
- }
- idx++;
- }
- return(cnt);
-
- /*******************************************
- * end of strochg *
- *******************************************/
-
- }